/*
 * MIT License
 *
 * Copyright (c) 2020 wen.gu <454727014@qq.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

 /***************************************************************************
  * Name: mp_log.cpp
  *
  * Purpose: log trace API define for micro player
  *
  * Developer:
  *   wen.gu , 2018-09-03
  *
  * TODO:
  *
  ***************************************************************************/

/******************************************************************************
**    INCLUDES
******************************************************************************/
#include <stdarg.h>
#include <vector>
#include <stdio.h>
#include <sstream>
#include <ctime>


#include "mp_log.h"
/******************************************************************************
**    MACROS
******************************************************************************/

/******************************************************************************
**    VARIABLE DEFINITIONS
******************************************************************************/


/******************************************************************************
**    FUNCTION DEFINITIONS
******************************************************************************/

MPLog::MPLog()
{
    //todo something
}

MPLog::~MPLog()
{
    //todo something
}

////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////

//static
MPLog& MPLog::getInstance()
{
    static MPLog ml;

    return ml;
}


////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////

void MPLog::logPrint(eMPLogLevel mll, 
                           const char* tag, 
                           const char * func, 
                           int line, 
                           const char * fmt, ...)
{   
    if (mll <= mLevel)
    {
        if (fmt)
        {
            char* tempBuf = new char[4096];
            va_list args;
            va_start(args, fmt);

            /** process args */

            vsnprintf(tempBuf, 4096 - 1, fmt, args);
            va_end(args);

            if (mCallback)
            {
                mCallback(mll,tag, func, line, tempBuf);
            }
            else
            {
                defaultPrint(mll, tag, func, line, tempBuf);
            } 

            delete tempBuf;
        }
    }    
}        

const char* MPLog::logLevel2ShortStr(eMPLogLevel mll)
{
    const char* type = "unknown";
    switch(mll)
    {
    case MPL_LevelError: type   = "ERR"; break;
    case MPL_LevelDebug: type   = "DBG"; break;
    case MPL_LevelInfo: type    = "INF"; break;
    case MPL_LevelWarning: type = "WRN"; break;
    }

    return type;
}

void MPLog::makeTimestampStr(char buf[], size_t len)
{
    
    if (mEnableTimestamp)
    {
        std::time_t t = std::time(nullptr);
        std::strftime(buf, len, "%D %T", std::gmtime(&t));
    }
    else
    {
        buf[0] = '\0';
    }    
}

void MPLog::makeLogPrefix(char buf[], size_t len, /** output buffer */
                         eMPLogLevel mll, const char* tag,
                         const char* func, int line)
{

    char timestampBuf[128];
    const char* type = logLevel2ShortStr(mll);
    /** process format param */
    makeTimestampStr(timestampBuf, sizeof(timestampBuf) - 1);

    snprintf(buf, len, "%s[%s][%s][%s.%d][%s]:", 
                 timestampBuf, mModuleName.c_str(), 
                 tag, func, line, type); 
}                             

////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////



void MPLog::defaultPrint(eMPLogLevel mll, const char* tag,
                                 const char* func, int line, const char* str)
{
    if (str)
    {
        char* buf = new char[512];
        makeLogPrefix(buf, 511, mll, tag, func, line);

        printf("%s%s", buf, str);

        delete buf;
    }
}                           
                           




void MPLog::setObserver(MPLogCallbackFunc cbFunc)
{
    mCallback = cbFunc;
}

void MPLog::setLogLevel(eMPLogLevel mll)
{
    mLevel = mll;
}

void MPLog::setModuleName(const char* name)
{
    if (name)
    {
        mModuleName = name;
    }
}

